package com.bitejiuyeke.controller;

import com.bitejiuyeke.common.AppResult;
import com.bitejiuyeke.common.ResultCode;
import com.bitejiuyeke.config.AppConfig;
import com.bitejiuyeke.model.Article;
import com.bitejiuyeke.model.User;
import com.bitejiuyeke.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 比特就业课
 */
@Api(tags = "帖子接口")
@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private IArticleService articleService;
    /**
     * 获取帖子列表
     * @param boardId 版块Id
     * @return AppResult<List<Article>>
     */
    @ApiOperation("获取帖子列表")
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId (@ApiParam("版块Id") @RequestParam(value = "boardId", required = false) Long boardId) {
        // 定义要返回的结果
        List<Article> results;

        if (boardId == null) {
            // 1. boardId为空时，获取所有的帖子
            results = articleService.selectAll();
        } else {
            // 2. boardId不为空时，获取指定版块下的帖子
            results = articleService.selectByBoardId(boardId);
        }
        // 防止返回的结果是null
        if (results == null) {
            results = new ArrayList<>();
        }
        // 返回结果
        return AppResult.success(results);
    }

    /**
     * 发布帖子
     * @param boardId 版块Id
     * @param title 标题
     * @param content 内容
     * @return AppResult
     */
    @ApiOperation("发布帖子")
    @PostMapping("/create")
    public AppResult create (HttpServletRequest request,
                             @ApiParam("版块Id") @RequestParam("boardId") @NonNull Long boardId,
                             @ApiParam("标题") @RequestParam("title") @NonNull String title,
                             @ApiParam("内容") @RequestParam("content") @NonNull String content) {
        // 获取当前登录用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION_KEY);
        // 判断用户状态
        if (user.getState() == 1) {
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 构造帖子对象
        Article article = new Article();
        article.setUserId(user.getId()); // 作者Id
        article.setBoardId(boardId); // 版块Id
        article.setTitle(title); // 标题
        article.setContent(content); // 内容
        // 调用Service
        articleService.create(article);
        // 返回结果
        return AppResult.success("发布成功");
    }

    @ApiOperation("获取帖子详情")
    @GetMapping("/details")
    public AppResult<Article> getDetails (HttpServletRequest request,
                                          @ApiParam("帖子Id") @RequestParam("id") @NonNull Long id) {
        // 获取当前登录用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION_KEY);
        // 调用Service
        Article article = articleService.selectDetailsById(id);
        // 判断当前登录用户是不是帖子的作者
        if (user.getId() == article.getUserId()) {
            // 设置所有者标识为true
            article.setOwn(true);
        }
        // 返回结果
        return AppResult.success(article);
    }

    @ApiOperation("更新帖子")
    @PostMapping("/modify")
    public AppResult modify (HttpServletRequest request,
                             @ApiParam("帖子Id") @RequestParam("id") @NonNull Long id,
                             @ApiParam("帖子标题") @RequestParam("title") @NonNull String title,
                             @ApiParam("帖子正文") @RequestParam("content") @NonNull String content) {
        // 1. 校验用户状态，禁言状态不能更新帖子
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION_KEY);
        if (user.getState() == 1) {
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 2. 查询帖子详情
        Article article = articleService.selectDetailsById(id);
        // 3. 校验帖子状态(是否删除，是否封贴)
        if (article == null || article.getDeleteState() == 1) {
            // 帖子不存在或已删除返回错误描述
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 4. 校验当前登录用户是不是帖子的作者
        if (user.getId() != article.getUserId()) {
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        // 调用Service
        articleService.modify(id, title, content);
        // 返回结果
        return AppResult.success();
    }
}
